<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据日期" prop="documentDate">
        <el-date-picker clearable
          v-model="queryParams.documentDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择单据日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="单据创建人" prop="documentCreator">
        <el-input
          v-model="queryParams.documentCreator"
          placeholder="请输入单据创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预算项目" prop="budgetItem">
        <el-input
          v-model="queryParams.budgetItem"
          placeholder="请输入预算项目 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报销总额" prop="totalReimbursementAmount">
        <el-input
          v-model="queryParams.totalReimbursementAmount"
          placeholder="请输入报销总额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否资产/材料" prop="isAssetMaterial">
         <el-select v-model="queryParams.isAssetMaterial" placeholder="请选择审批状态" clearable>
                    <el-option
                      v-for="dict in dict.type.paymentallcation_is_asset_material"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用承担部门" prop="supplierDepartment">
        <el-input
          v-model="queryParams.supplierDepartment"
          placeholder="请输入费用承担部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批状态" prop="approveStatus">
       <el-select v-model="queryParams.approveStatus" placeholder="请选择审批状态" clearable>
                    <el-option
                      v-for="dict in dict.type.yy_bip_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sci:pamentdata:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sci:pamentdata:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sci:pamentdata:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sci:pamentdata:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pamentdataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据日期" align="center" prop="documentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.documentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据创建人" align="center" prop="documentCreator" />
      <el-table-column label="预算项目" align="center" prop="budgetItem" />
      <el-table-column label="报销总额" align="center" prop="totalReimbursementAmount" />
      <el-table-column label="是否资产/材料" align="center" prop="isAssetMaterial" >
       <template slot-scope="scope">
            <dict-tag :options="dict.type.paymentallcation_is_asset_material" :value="scope.row.isAssetMaterial"/>
          </template>
          </el-table-column>
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="费用承担部门" align="center" prop="supplierDepartment" />
      <el-table-column label="审批状态" align="center" prop="approveStatus" >
         <template slot-scope="scope">
            <dict-tag :options="dict.type.yy_bip_status" :value="scope.row.approveStatus"/>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['sci:purchaseRequirement:query']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改存储付款申请单核心信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据编号 - 唯一业务标识符" prop="documentNumber">
          <el-input v-model="form.documentNumber" placeholder="请输入单据编号 - 唯一业务标识符" />
        </el-form-item>
        <el-form-item label="单据日期 - 单据创建日期" prop="documentDate">
          <el-date-picker clearable
            v-model="form.documentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择单据日期 - 单据创建日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="费用承担组织 - 费用归属部门或组织" prop="costBearingOrganization">
          <el-input v-model="form.costBearingOrganization" placeholder="请输入费用承担组织 - 费用归属部门或组织" />
        </el-form-item>
        <el-form-item label="单据创建人 - 制单人姓名" prop="documentCreator">
          <el-input v-model="form.documentCreator" placeholder="请输入单据创建人 - 制单人姓名" />
        </el-form-item>
        <el-form-item label="预算项目 - 关联的预算项目" prop="budgetItem">
          <el-input v-model="form.budgetItem" placeholder="请输入预算项目 - 关联的预算项目" />
        </el-form-item>
        <el-form-item label="资金来源 - 资金渠道类型" prop="fundSource">
          <el-input v-model="form.fundSource" placeholder="请输入资金来源 - 资金渠道类型" />
        </el-form-item>
        <el-form-item label="附件数 - 关联附件数量" prop="attachmentCount">
          <el-input v-model="form.attachmentCount" placeholder="请输入附件数 - 关联附件数量" />
        </el-form-item>
        <el-form-item label="报销总额 - 总报销金额" prop="totalReimbursementAmount">
          <el-input v-model="form.totalReimbursementAmount" placeholder="请输入报销总额 - 总报销金额" />
        </el-form-item>
        <el-form-item label="付款总额 - 实际付款金额" prop="totalPaymentAmount">
          <el-input v-model="form.totalPaymentAmount" placeholder="请输入付款总额 - 实际付款金额" />
        </el-form-item>
        <el-form-item label="报销说明 - 详细说明文字" prop="reimbursementDescription">
          <el-input v-model="form.reimbursementDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否资产/材料: 0-否, 1-是" prop="isAssetMaterial">
          <el-input v-model="form.isAssetMaterial" placeholder="请输入是否资产/材料: 0-否, 1-是" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-input v-model="form.supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="费用承担部门" prop="supplierDepartment">
          <el-input v-model="form.supplierDepartment" placeholder="请输入费用承担部门" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

     <!-- 查看抽屉 -->
    <el-drawer
      title="付款申请详情"
      :visible.sync="drawer"
      size="50%"
      direction="rtl"
    >

      <!-- 自定义标题插槽 -->
  <div slot="title" class="drawer-title">
    <span>采购需求详情</span>
    <el-tag 
      :type="getStatusTagType(formView.approveStatus)"
      size="medium"
      style="margin-left: 10px;"
    >
      <dict-tag 
        :options="dict.type.yy_bip_status" 
        :value="formView.approveStatus"
      />
    </el-tag>
  </div>
      <!-- 第一部分 - 指定字段分组 -->
      <el-card shadow="always" style="margin: 20px;">
        <el-descriptions title="基本信息" :column="2" border>
           <el-descriptions-item label="单据编号">{{ formView.documentNumber }}</el-descriptions-item>
           <el-descriptions-item label="单据日期">{{ parseTime(formView.documentDate, '{y}-{m}-{d}') }}</el-descriptions-item>
          <el-descriptions-item label="费用承担组织">{{ formView.costBearingOrganization }}</el-descriptions-item>
          <el-descriptions-item label="经办人">{{ formView.documentCreator }}</el-descriptions-item>
          <el-descriptions-item label="预算项目">{{ formView.budgetItem }}</el-descriptions-item>
          <el-descriptions-item label="资金来源">{{ formView.fundSource }}</el-descriptions-item>
           <el-descriptions-item label="附件数">{{ formView.attachmentCount }}</el-descriptions-item>
          <el-descriptions-item label="报销总额">{{ formView.totalReimbursementAmount }}</el-descriptions-item>
               <el-descriptions-item label="是否资产/材料">
            <dict-tag :options="dict.type.paymentallcation_is_asset_material" :value="formView.isAssetMaterial"/>
          </el-descriptions-item>
           <el-descriptions-item label="付款总额">{{ formView.totalPaymentAmount }}</el-descriptions-item>
           <el-descriptions-item label="报销说明">{{ formView.reimbursementDescription }}</el-descriptions-item>
          <el-descriptions-item label="费用承担部门">{{ formView.supplierDepartment }}</el-descriptions-item>
          <el-descriptions-item label="供应商名称">{{ formView.supplier }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import { listPamentdata, getPamentdata, delPamentdata, addPamentdata, updatePamentdata } from "@/api/sci/pamentdata";

export default {
  name: "Pamentdata",
  dicts:['yy_bip_status','paymentallcation_is_asset_material'],
  data() {
    return {
       // ... 其他数据保持不变 ...
      drawer: false, // 控制抽屉显示
      formView: {},   // 查看表单数据
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 存储付款申请单核心信息表格数据
      pamentdataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        documentDate: null,
        documentCreator: null,
        budgetItem: null,
        totalReimbursementAmount: null,
        isAssetMaterial: null,
        supplier: null,
        supplierDepartment: null,
        approveStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        documentNumber: [
          { required: true, message: "单据编号 - 唯一业务标识符不能为空", trigger: "blur" }
        ],
        documentDate: [
          { required: true, message: "单据日期 - 单据创建日期不能为空", trigger: "blur" }
        ],
        costBearingOrganization: [
          { required: true, message: "费用承担组织 - 费用归属部门或组织不能为空", trigger: "blur" }
        ],
        documentCreator: [
          { required: true, message: "单据创建人 - 制单人姓名不能为空", trigger: "blur" }
        ],
        totalReimbursementAmount: [
          { required: true, message: "报销总额 - 总报销金额不能为空", trigger: "blur" }
        ],
        totalPaymentAmount: [
          { required: true, message: "付款总额 - 实际付款金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查看按钮操作 */
    handleView(row) {
      const id = row.id || this.ids;
      getPamentdata(id).then(response => {
        this.formView = response.data;
        this.drawer = true;
      });
    },
    
    // 时间格式化方法
    parseTime(time, format = '{y}-{m}-{d}') {
      if (!time) return '';
      let date = new Date(time);
      const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds()
      };
      return format.replace(/{([ymdhisa])+}/g, (result, key) => {
        const value = formatObj[key];
        return value.toString().padStart(2, '0');
      });
    },
    /** 查询存储付款申请单核心信息列表 */
    getList() {
      this.loading = true;
      listPamentdata(this.queryParams).then(response => {
        this.pamentdataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        documentNumber: null,
        documentDate: null,
        costBearingOrganization: null,
        documentCreator: null,
        budgetItem: null,
        fundSource: null,
        attachmentCount: null,
        totalReimbursementAmount: null,
        totalPaymentAmount: null,
        reimbursementDescription: null,
        isAssetMaterial: null,
        supplier: null,
        supplierDepartment: null,
        approveStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加存储付款申请单核心信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPamentdata(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改存储付款申请单核心信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePamentdata(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPamentdata(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除存储付款申请单核心信息编号为"' + ids + '"的数据项？').then(function() {
        return delPamentdata(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sci/pamentdata/export', {
        ...this.queryParams
      }, `pamentdata_${new Date().getTime()}.xlsx`)
    },
      getStatusTagType(status) {
    switch(status) {
      case 0: return 'info';    // 未提交
      case 3: return 'warning'; // 审批中
      case 1: return 'success'; // 审批通过
      default: return 'info';
    }
  },
  }
};
</script>
<style scoped>
/* 添加抽屉内容的样式 */
.el-drawer__body {
  padding: 0;
  overflow-y: auto;
}

.el-card {
  margin-bottom: 15px;
}

.el-descriptions__title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
}
</style>